﻿namespace Shanda.DevFx.Security.Web.Pages
{
    using Shanda.DevFx.Core;
    using Shanda.DevFx.Data.Attributes;
    using Shanda.DevFx.Data.Utils;
    using Shanda.DevFx.Security;
    using Shanda.DevFx.Security.Web;
    using Shanda.DevFx.Utils;
    using System;
    using System.Web.UI.WebControls;

    public class RoleEditPage : Shanda.DevFx.Security.Web.BasePage
    {
        protected Button btnSubmit;
        protected CheckBox cbxAddMeToRole;
        [Column("IsAvailable", ColumnType=typeof(bool))]
        protected DropDownList ddlIsAvailable;
        [Column("IsInnerRole", ColumnType=typeof(bool))]
        protected DropDownList ddlIsInnerRole;
        protected Label lblMessage;
        private bool newItem;
        protected RequiredFieldValidator rfvRoleNo;
        protected RequiredFieldValidator rfvTitle;
        private string roleNo;
        [Column("Description")]
        protected TextBox txtDescription;
        protected TextBox txtRoleNo;
        [Column("Title")]
        protected TextBox txtTitle;
        protected ValidationSummary vsRole;

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (!base.get_IsValid())
            {
                this.lblMessage.set_Text("请正确填写表单");
            }
            else
            {
                Role item;
                if (this.newItem)
                {
                    item = Securitor.RoleManager.CreateItem();
                    item.RoleNo = this.txtRoleNo.get_Text();
                }
                else
                {
                    item = Securitor.RoleManager.GetItem(this.roleNo);
                }
                DataTransfer.TransWebFormDataToEntityProperty(this, item);
                IAOPResult result = item.Save();
                string resultDescription = "成功保存";
                if (result.ResultNo < 0)
                {
                    resultDescription = result.ResultDescription;
                }
                else if (this.cbxAddMeToRole.get_Checked())
                {
                    result = Securitor.Authenticator.AddUserRole(base.get_User().get_Identity().get_Name(), item.RoleNo);
                    if (result.ResultNo < 0)
                    {
                        resultDescription = "角色建立成功，但添加用户到角色失败：" + result.ResultDescription;
                    }
                }
                this.lblMessage.set_Text(resultDescription);
            }
        }

        private void InitializeComponent()
        {
            this.btnSubmit.add_Click(new EventHandler(this.btnSubmit_Click));
            base.add_Load(new EventHandler(this.Page_Load));
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
        }

        private void Page_Load(object sender, EventArgs e)
        {
            this.roleNo = base.get_Request().get_Params()["RoleNo"];
            this.newItem = Checker.CheckEmptyString("RoleNo", this.roleNo, false);
            if (!base.get_IsPostBack() && !this.newItem)
            {
                Role item = Securitor.RoleManager.GetItem(this.roleNo);
                if (item == null)
                {
                    WebHelper.ShowMessage("没找到此角色，或者您没有权限操作", "history.back()", true, true);
                }
                this.txtRoleNo.set_Text(item.RoleNo);
                this.txtRoleNo.set_ReadOnly(true);
                DataTransfer.TransEntityPropertyToWebFormData(item, this);
                this.cbxAddMeToRole.set_Checked(false);
                this.cbxAddMeToRole.set_Enabled(false);
            }
        }
    }
}

